from google.appengine.ext import db
import simplejson as json

class DishType(db.Model):
    name = db.StringProperty(required=True)
    img = db.BlobProperty()

class Restaurant(db.Model):
    location = db.ListProperty(db.PostalAddress)
    locationLong = db.ListProperty(float)
    locationLat = db.ListProperty(float)
    telephone = db.PhoneNumberProperty()
    name = db.StringProperty(required=True)
 
class Dish(db.Model):
    name = db.StringProperty(required=True)
    av_rate = db.RatingProperty(required=True)#restrict the rating
    no_rate= db.IntegerProperty(required=True)
    img = db.BlobProperty()
    dishType = db.ReferenceProperty(DishType, collection_name='dishes')
    restaurant = db.ReferenceProperty(Restaurant, collection_name='dishes')
    ingredients = db.ListProperty(db.Key)
    comments=db.StringListProperty()
    def dish_to_json(self):
                return json.dumps({
                        'name': str(self.name)
                }) 

class IngredientType(db.Model):
    name = db.StringProperty(required=True)
    
class User(db.Model):
    android_id = db.StringProperty(required=True)
    favDishes = db.ListProperty(db.Key)
# Many to many user and dish for the rate
class User_Dish(db.Model):
    user = db.ReferenceProperty(User, collection_name='users')
    dish = db.ReferenceProperty(Dish, collection_name='dishes')
    rate = db.RatingProperty(required=True)
    
    
class Ingredient(db.Model):
    name = db.StringProperty(required=True)
    #img = db.StringProperty(required=True)
    ingredientType = db.ReferenceProperty(IngredientType, collection_name='ingredients')
    #This method is to return all dishes having a certain ingredient
    fileName=db.StringProperty()
    @property
    def dishes(self):
        return Dish.gql("WHERE ingredients = :1", self.key())
    
